<template>
  <div>
    <!-- 经销商名称\代码搜索 -->
    <TopFilter :list="topFilterInit" :cols="2" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <!-- <FilterTable
      height="250"
      ref="listModal"
      :columns="columns"
      :selectionType="selectType"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="selectChange"
      columnsRef="cms-common_dealerdata_indexWIthType_01"
    ></FilterTable> -->
    <FilterTable
      height="250"
      ref="listModal"
      :columns="columns"
      :selectionType="selectType"
      :dataSource="dealerDataSource"
      :datakey="'dealerCode'"
      columnsRef="cms-common_dealerdata_indexWIthType_01"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isMemoryPagination="true"
    ></FilterTable>
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">确定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getDealerList, allDealerInfoList, getRegion, getCommunity } from '@/api/cms-common/dealerdata';
import { getAllList } from '@/api/cms-api/origin/originInfo';
import { DICT_CODE } from '@/api/cms-api/origin/commonDealerType'; //经销商类型工具类
import { region } from '@/api/cms-common/region'; //行政区域
import { findFinanceCompany } from '@/api/cms-api/origin/originInfo/OEM';
import autoLoding from '@/utils/loding'; //等待框
import { getLevelOneArea1 } from '@/api/cms-common/dealerdata/index'; //事业部，办事处

export default {
  mixins: [dictionary, region, autoLoding],
  props: {
    value: {
      type: Object,
      required: true,
    },
    selectType: {
      type: String,
      default: 'multiple',
    },
  },
  data() {
    return {
      excludeBigCustomer: this.value.excludeBigCustomer ? 25381006 : null,
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      dealerDataSource: [],
      columns: this.creatHeader(),
      //刷新初始页面
      fetchApi: getAllList,
      ORG_TYPE_SERVICE_SMALL_AREA: "15061015,15061021", //销售小区
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
    };
  },
  mounted() {
    console.log(this.ORG_TYPE_SERVICE_SMALL_AREA)
    this.getTopLevelArea(this.ORG_TYPE_SERVICE_SMALL_AREA, 'serviceSmallArea');
  },
  methods: {
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: '代码', fieldName: 'dealerCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '简称', fieldName: 'dealerShortname', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '服务小区', fieldName: 'serviceSmallArea', placeholder: '请选择', initialValue: '', itemList: [] },
        // { type: 'INPUT', label: '区域', fieldName: 'quyu',placeholder: '请输入', initialValue:''},
        { type: 'INPUT', label: '省份', fieldName: 'provinceName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '城市', fieldName: 'cityName', placeholder: '请输入', initialValue: '' },
        // { type: 'INPUT', label: '服务站类型', fieldName: 'serviceShopType',initialValue: [], itemList: this.createDictList('1621') },//SERVICE_SHOP_TYPE
        { type: 'SELECT', label: '服务站类型', fieldName: 'serviceShopType', placeholder: '请选择', initialValue: '', itemList: this.createDictList('1621') },
        // {
        // 	type: 'SELECT',
        // 	label: '经销商形式',
        // 	fieldName: 'dealerAuth',
        // 	initialValue: [],
        // 	itemList: this.createDictList('5008') //复选框数据源
        // },
      ];
    },

    //获取顶层组织数据
    async getTopLevelArea(areaType, filedName) {
      console.log(areaType, filedName);
      const res = await getLevelOneArea1({orgType:areaType});
      if (res.resultCode === 200) {
        this.a = res.data;
        this.topFilterInit.find((item) => item.fieldName === filedName).itemList = res.data.map((x) => ({ value: parseInt(x.id), text: x.serviceSmallArea }));
      }
    },
    //搜索获取数据
    changeHandle(val) {
      this.$refs.listModal.$refs.pageTable.START_LOADING();
      let extP = {};
      this.fetchParams = Object.assign({}, val, extP);
      this.getDealerData();
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },

        //  {title: '区域', dataIndex: 'quyu',sorter:true},
        {title: '区域' ,dataIndex:'serviceSmallArea',sorter:true },
        { title: '省份', dataIndex: 'provinceName', sorter: true },
        { title: '城市', dataIndex: 'cityName', sorter: true },
        { title: '公司全称', dataIndex: 'dealerCompanyName', sorter: true },
        { title: '代码', dataIndex: 'dealerCode', sorter: true },
        { title: '简称', dataIndex: 'dealerShortname', sorter: true },
        //{ title: '服务站类型', dataIndex: 'serviceShopType', sorter: true },
        {
          title: '服务站类型',
          dataIndex: 'serviceShopType',
          sorter: true,
          filterItems: this.createDictList('1621'),
          render: (props) => {
            return <span>{this.createDictText(props.row.serviceShopType, '1621')}</span>;
          },
        },
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    saveDialog() {
      console.log('excludeBigCustomer:' + this.excludeBigCustomer);
      if (this.selectedRows.length == 0) {
        this.$notify.warning({ title: '提示信息', message: '请勾选经销商!' });
        return;
      }
      this.$emit('close', { visible: false, data: this.selectedRows });
    },
    //取消操作
    closeDialog() {
      // this.$emit('close', { visible: false ,"data":this.selectedRows});
      this.$emit('close', { visible: false });
    },
    //加载事业部数据
    async getDeptData() {
      const res = await getRegion({});
      if (res.resultCode === 200) {
        this.topFilterInit.find((item) => item.fieldName === 'deptId').itemList = res.data.map((x) => ({ value: x.id, text: x.orgName }));
      }
    },
    async getFinanceCompany() {
      const res = await findFinanceCompany();
      console.log('123', res);
      console.log('2', this.topFilterInit[3]);
      if (res.resultCode === 200) {
        this.topFilterInit[3].itemList = res.data.map((x) => ({ value: x.financeCompanyId, text: x.financeCompanyName }));
      }
    },
    //加载经销商数据
    async getDealerData() {
      let val = this.fetchParams;
      let extP = {
        orgTypes: this.value.orgTypes,
      };
      let vals = Object.assign({}, val, extP);
      let resp = await getAllList(vals);
      if (resp.resultCode === 200) {
        this.dealerDataSource = resp.data;
      }
      this.$refs.listModal.DO_REFRESH(true);
      this.$refs.listModal.$refs.pageTable.STOP_LOADING();
    },
  },
};
</script>

<style lang="less" scoped>
</style>
